package com.wyt.controller.role;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wyt.dao.RoleMapper;
import com.wyt.entity.Role;
import com.wyt.until.CommonResult;
import com.wyt.until.TableLayui;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

//
@RestController
@RequestMapping("role")
public class RoleController {
    @Autowired
    private RoleMapper roleMapper;
    @RequestMapping("findAll")
    public TableLayui findAll(String rName, Integer page, Integer limit){
        PageHelper.startPage(page,limit);
        List<Role> list=roleMapper.selectAll(rName);
        PageInfo pageInfo = new PageInfo(list);
        return new TableLayui(0,"",pageInfo.getTotal(),pageInfo.getList());
    }
    @RequestMapping("update")
    public CommonResult updateById(Role role){
        return new CommonResult(0,"",null);
    }
    @RequestMapping("/insert")
    public CommonResult insert(Role role) {
        int i = 0;
        if (role.getId() == null) {
            i = roleMapper.insert(role);
        } else {
            i = roleMapper.update(role);
        }
        if (i > 0) {
            CommonResult commonResult = new CommonResult(200, "", null);
            return commonResult;
        } else {
            CommonResult commonResult = new CommonResult(0, "", null);
            return commonResult;
        }
    }

    @RequestMapping("delete")
    public CommonResult delete(int id){
        int j =roleMapper.delete(id);
        if (j > 0) {
            CommonResult commonResult = new CommonResult(200, "", null);
            return commonResult;
        } else {
            CommonResult commonResult = new CommonResult(0, "", null);
            return commonResult;
        }
    }
}
